<template>
        <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
        >
            <el-form-item label="原始密码" prop="oldPass">
                <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="updatePasswordVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm(ruleForm)">确 定</el-button>
            </el-form-item>
        </el-form>
</template>

<script>
export default {
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else if (value.length < 6 || value.length > 32) {
                callback(new Error("密码长度必须在6-32位之间"));
            } else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                oldPass: "",
                pass: "",
                checkPass: "",
                rules: {
                    oldPass: [
                        {
                            required: true,
                            message: "请输入密码",
                            trigger: "blur"
                        }
                    ],
                    pass: [{ validator: validatePass, trigger: "blur" }],
                    checkPass: [{ validator: validatePass2, trigger: "blur" }]
                },
                updatePasswordVisible: false
            }
        };
    },
    methods: {
        submitForm(data) {
            this.$refs["ruleForm"].validate(valid => {
                if (valid) {
                    let { pass, oldPass } = data;
                    this.$http("/api/userInfo/updataPass", {
                        params: { pass, oldPass }
                    }).then(res => {
                        if (res.data.code === 0) {
                            this.updatePasswordVisible = false;
                        }
                        this.$message({
                            message: res.data.msg,
                            type: res.data.type
                        });
                    });
                }
            });
        }
    }
};
</script>
